﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Basic;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Basic
{
    /// <summary>
    /// 根据生产批次获取溯源基本信息请求命令处理类
    /// </summary>
    public class GetBasic_TraceabilityByProduction_BatchHandler:IRequestHandler<GetBasic_TraceabilityByProduction_BatchCommand,ApiResult<BasicDTO>>
    {
        IBaseRepository<Basic_Traceability> basicRep;
        IBaseRepository<Goods_Information> goodsRep;

        public GetBasic_TraceabilityByProduction_BatchHandler(IBaseRepository<Basic_Traceability> basicRep,IBaseRepository<Goods_Information> goodsRep)
        {
            this.basicRep = basicRep;
            this.goodsRep = goodsRep;
        }

        public Task<ApiResult<BasicDTO>> Handle(GetBasic_TraceabilityByProduction_BatchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<BasicDTO> res = new ApiResult<BasicDTO>();

                var basic=basicRep.GetAll().FirstOrDefault(x=>x.Production_Batch==request.production_Batch&&!x.IsDeleted);
                var goods = goodsRep.GetAll().FirstOrDefault(x=>x.Id==basic.GoodsId);
                var dto=new BasicDTO
                {
                    Id = basic.Id,
                    GoodsId = basic.GoodsId,
                    GoodsName = goods.GoodsName,
                    Production_Batch = basic.Production_Batch,
                    Quality_Guarantee = basic.Quality_Guarantee,
                    Production_License = basic.Production_License,
                    Manufacturing_Enterprise = basic.Manufacturing_Enterprise,
                    Information_Completeness = basic.Information_Completeness,
                    Cover_Image = basic.Cover_Image,
                    CreateTime = basic.CreateTime,
                    Creator = basic.Creator,
                    UpdateTime = basic.UpdateTime,
                    Reviser = basic.Reviser
                };
                res.Msg = "根据生产批次获取溯源基本信息";
                res.Code = ApiEnum.Success;
                res.Data = dto;
                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
